package com.zlict.admin.service.impl;

import com.zlict.admin.domain.ProjectIntangibleassets;
import com.zlict.admin.mapper.ProjectIntangibleassetsMapper;
import com.zlict.admin.service.IProjectIntangibleassetsService;
import com.zlict.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

import static com.zlict.common.utils.SecurityUtils.getUserId;

/**
 * 项目无形资产管理Service业务层处理
 * 
 * @author dzw
 * @date 2025-06-16
 */
@Service
public class ProjectIntangibleassetsServiceImpl implements IProjectIntangibleassetsService 
{
    @Autowired
    private ProjectIntangibleassetsMapper projectIntangibleassetsMapper;

    /**
     * 查询项目无形资产管理
     * 
     * @param id 项目无形资产管理主键
     * @return 项目无形资产管理
     */
    @Override
    public ProjectIntangibleassets selectProjectIntangibleassetsById(Long id)
    {
        return projectIntangibleassetsMapper.selectProjectIntangibleassetsById(id);
    }

    /**
     * 查询项目无形资产管理列表
     * 
     * @param projectIntangibleassets 项目无形资产管理
     * @return 项目无形资产管理
     */
    @Override
    public List<ProjectIntangibleassets> selectProjectIntangibleassetsList(ProjectIntangibleassets projectIntangibleassets)
    {
        return projectIntangibleassetsMapper.selectProjectIntangibleassetsList(projectIntangibleassets);
    }

    /**
     * 新增项目无形资产管理
     * 
     * @param projectIntangibleassets 项目无形资产管理
     * @return 结果
     */
    @Override
    public int insertProjectIntangibleassets(ProjectIntangibleassets projectIntangibleassets)
    {

        if (ObjectUtils.isEmpty(projectIntangibleassets)){
            throw new RuntimeException("参数错误");
        }
        if (projectIntangibleassets.getProjectId() == null){
            throw new RuntimeException("未获取到项目,添加失败");
        }
        projectIntangibleassets.setCreateTime(DateUtils.getNowDate());
        projectIntangibleassets.setCreateBy(getUserId());
        if (projectIntangibleassets.getIntangibleassetsIds() == null || projectIntangibleassets.getIntangibleassetsIds().size() == 0){
//            projectDevice.setCreateTime(DateUtils.getNowDate());
//            projectDevice.setCreateBy(getUserId());
//            return projectDeviceMapper.insertProjectDevice(projectDevice);
            throw new RuntimeException("未选择无形资产,不可添加");
        }else{
            int num = projectIntangibleassets.getIntangibleassetsIds().size();
            int sum = 0;
            for (int i = 0; i < num; i++) {
                projectIntangibleassets.setIntangibleassetsId(projectIntangibleassets.getIntangibleassetsIds().get(i));
                sum += projectIntangibleassetsMapper.insertProjectIntangibleassets(projectIntangibleassets);
            }
            return sum;
        }

    }

    /**
     * 修改项目无形资产管理
     * 
     * @param projectIntangibleassets 项目无形资产管理
     * @return 结果
     */
    @Override
    public int updateProjectIntangibleassets(ProjectIntangibleassets projectIntangibleassets)
    {
        projectIntangibleassets.setUpdateTime(DateUtils.getNowDate());
        projectIntangibleassets.setUpdateBy(getUserId());
        return projectIntangibleassetsMapper.updateProjectIntangibleassets(projectIntangibleassets);
    }

    /**
     * 批量删除项目无形资产管理
     * 
     * @param ids 需要删除的项目无形资产管理主键
     * @return 结果
     */
    @Override
    public int deleteProjectIntangibleassetsByIds(Long[] ids)
    {
        return projectIntangibleassetsMapper.deleteProjectIntangibleassetsByIds(ids);
    }

    /**
     * 删除项目无形资产管理信息
     * 
     * @param id 项目无形资产管理主键
     * @return 结果
     */
    @Override
    public int deleteProjectIntangibleassetsById(Long id)
    {
        return projectIntangibleassetsMapper.deleteProjectIntangibleassetsById(id);
    }
}
